package com.gy.zpchat.ui.friend;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.google.gson.Gson;

import im.zego.zim.ZIM;
import im.zego.zim.callback.ZIMFriendAddedCallback;
import im.zego.zim.callback.ZIMFriendApplicationSentCallback;
import im.zego.zim.entity.ZIMError;
import im.zego.zim.entity.ZIMFriendAddConfig;
import im.zego.zim.entity.ZIMFriendApplicationInfo;
import im.zego.zim.entity.ZIMFriendApplicationSendConfig;
import im.zego.zim.entity.ZIMFriendInfo;

import com.gy.zpchat.R;
import com.gy.zpchat.util.AppLogger;
import com.gy.zpchat.util.widget.CustomDialog;

public class ZIMAddFriendActivity extends Activity {

    private ZIMFriendAddConfig config;

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, ZIMAddFriendActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_friend);
    }

    CustomDialog dialog;

    public void sendFriendRequest(View view) {
        dialog = CustomDialog.createDialog("申请好友中...", this);
        dialog.show();
        EditText editText = findViewById(R.id.user_id);
        String userID = editText.getText().toString();
        EditText wordingText = findViewById(R.id.wording);
        EditText aliasText = findViewById(R.id.alias);
        EditText key = findViewById(R.id.key);
        EditText value = findViewById(R.id.value);
        ZIMFriendApplicationSendConfig config = new ZIMFriendApplicationSendConfig();
        config.friendAlias = aliasText.getText().toString();
        config.wording = wordingText.getText().toString();
        ZIM.getInstance().sendFriendApplication(
            userID, config, new ZIMFriendApplicationSentCallback() {
                    @Override
                    public void onFriendApplicationSentCallback(ZIMFriendApplicationInfo applicationInfo, ZIMError errorInfo) {

                    AppLogger.getInstance().d("onSendFriendApplicationCallback friendInfoList: %s  errorInfo: %s",
                            new Gson().toJson(applicationInfo) ,  new Gson().toJson(errorInfo) );
                    Toast.makeText(ZIMAddFriendActivity.this, errorInfo.message, Toast.LENGTH_LONG)
                        .show();
                    dialog.cancel();
                    finish();
                }
            });
    }

    public void addFriend(View view) {
        dialog = CustomDialog.createDialog("直接添加好友中...", this);
        dialog.show();
        EditText editText = findViewById(R.id.user_id);
        String userID = editText.getText().toString();
        EditText wordingText = findViewById(R.id.wording);
        EditText aliasText = findViewById(R.id.alias);
        EditText key = findViewById(R.id.key);
        EditText value = findViewById(R.id.value);
        ZIMFriendAddConfig config = new ZIMFriendAddConfig();
        config.friendAlias = aliasText.getText().toString();
        config.wording = wordingText.getText().toString();
        ZIM.getInstance().addFriend(
                userID, config, new ZIMFriendAddedCallback() {
                    @Override
                    public void onFriendAddedCallback(ZIMFriendInfo friendInfo, ZIMError zimError) {
                        AppLogger.getInstance().d("onFriendAddedCallback friendInfo: %s  errorInfo: %s",
                                new Gson().toJson(friendInfo),  new Gson().toJson(zimError) );
                        Toast.makeText(ZIMAddFriendActivity.this, zimError.message, Toast.LENGTH_LONG)
                                .show();
                        dialog.cancel();
                        finish();
                    }
                });
    }


}
